import Event from "../../utils/data/event"
import School from "../../utils/data/school"
import Road from "../../utils/data/road"
import Home from "../../utils/data/home"
import Night from "../../utils/data/night"

const Base64 = require('../../utils/data/imgBase64.js');
const app = getApp()
var events,timer,sceneId,n,m,id//events为初始事件，prop为各事件对象，n用于游戏场景事件集合里的事件拆分，id是下一个事件的id,m是计数
var achievements = []//成就集合
Page({
  /**
   * 页面的初始数据
   */
  data: {
    background_image:'',
    show: 0,//界面基本属性
    chose1: '',//按钮选项
    chose2: '',
    visible: false,//两个选择的按钮的可见性
    end: false,//是否结局
    eve:'',//单个子事件
    prop: '',//属性
    imgBase64: Base64.imgArray
  },

  onLoad: function (scene) {
    sceneId = scene.id
    switch(sceneId){
      case '1': 
        events = School.slice(0)//拆分数组
        this.data.background_image = this.data.imgBase64[0]
        //'https://636c-cloud1-2gydc1w42610d7b4-1306724493.tcb.qcloud.la/school.jpg?sign=d592108cbe1ce73c499aab1dfe9267e1&t=1654243998'
        break
      case '2': 
        events = Home.slice(0)
        this.data.background_image = this.data.imgBase64[1]
        //'https://636c-cloud1-2gydc1w42610d7b4-1306724493.tcb.qcloud.la/home.jpg?sign=ad19bfad3f011a3f009e84ac1e950600&t=1654243359'
        break
      case '3': 
        events = Road.slice(0)
        this.data.background_image = this.data.imgBase64[3]
        //'https://636c-cloud1-2gydc1w42610d7b4-1306724493.tcb.qcloud.la/road.jpg?sign=3bea23a16eacdc6d9d332abde72288d6&t=1654243383'
        break
      case '4': 
        events = Event.slice(0)
        this.data.background_image = this.data.imgBase64[2]
        //'https://636c-cloud1-2gydc1w42610d7b4-1306724493.tcb.qcloud.la/process.jpg?sign=42dfc441f8d7b520b5bd5449afb80c46&t=1654243370'
        break
      case '5':
        events = Night.slice(0)
        this.data.background_image = this.data.imgBase64[4]
        //'https://636c-cloud1-2gydc1w42610d7b4-1306724493.tcb.qcloud.la/night.jpg?sign=c3d04ae9ba8c99dce7885aec81aac08c&t=1654246125'
    }
    this.data.prop = events[0]
    id = 0; n = 1; m = 0;
    try {
      this.setData({
        background_image:this.data.background_image
      })
      let value = wx.getStorageSync('achievement')//同步调用
      if (value) {
          achievements = value//读取数据是为了防止有重复的成就获得然后添加进本地中
       }
      } catch (e) {
        console.log(e);
    }
  },
  onReady(){
    let that = this
    setTimeout(function () {
      that.setData({
        eve: that.data.prop.events.slice(0,1)//图片加载完后再出现文字,view没有bindload,用onready和延时来提高文字出现的时间，时间不能过长，否则也会影响体验
      })   
     }, 200) //延迟时间 这里是0.2秒
   
  },
  next:function(){
    if(n < this.data.prop.events.length){
      this.setData({
        visible: false,//每一次都要点击都要更新
        eve: this.data.prop.events.slice(n,n+1),
      })
      n++
      //事件处理函数，把相关事件全部合在一个数组中再进行处理，以按钮点击事件作为上一个事件和下一个事件的分界点，同时也方便了后期如果修改的便利性 
    }else if('branch' in this.data.prop){//有branch属性在说明需要使用按钮进行选择了
      this.setData({
        chose1: this.data.prop.branch.chose1[0],
        chose2: this.data.prop.branch.chose2[0],
        visible: true
      })//如果失败了就把else if这一块全部删掉就行
    }else if(sceneId == '2' && 'include' in this.data.prop){
      let i = 0
      for(i = 0;i < this.data.prop.include.length;i++){
        if(this.data.prop.include[i][0] == app.globalData.propId && app.globalData.propId != 1 && m == 0){
        m++;//这里m++的意义是为了让重新开始按钮提前浮现
        this.data.eve=this.data.prop.include[i][1]
        }else if(this.data.prop.include[i][0] == app.globalData.propId && app.globalData.propId == 1 && m == 0){
        m++;//这里m++的意义是为了让重新开始按钮提前浮现
        Math.floor(Math.random()*10) < 5?this.data.eve=this.data.prop.include[i][1]:this.data.eve=this.data.prop.include[i][2]//此处是应急包的随机性
      }
     }
     if(m == 0){
        m++
        this.data.eve = this.data.prop.include[i-1][1]//默认事件
      }
        this.setData({
          eve:this.data.eve
        })  
      }
    if('isEnd' in this.data.prop && n == this.data.prop.events.length){//判断是否到达了结局
      if('include' in this.data.prop && m==1){
        this.setData({
          end: true
        })
      }else if('include' in this.data.prop == false){
        this.setData({
          end: true
        })
      }
    }
  },

  end:function(){
    this.data.prop = events[0]
    n = 0;id = 0;m = 0 //属性重置
      this.setData({
       chose1: '',
       chose2: '',
       eve:'',
       end: false
    })
  },

  change:function(e){
    n = 0;//下一个事件集要把n归0
    if(e.currentTarget.dataset.id == 1){
      id = this.data.prop.branch.chose1[1] 
      this.ach()
    }else{
      id = this.data.prop.branch.chose2[1]
      this.ach()
    }
},

  ach:function() {//构造一个复用的函数
   this.data.prop = events.find(item=>item.id == id)//寻找下一个事件对象的id，并赋值给prop和调用next进行下一个事件，让衔接看起来自然
   if('ach' in this.data.prop){//如果有成就对象在prop对象中
    let data = achievements
    data.find(item=>item.achievementId == this.data.prop.ach.achievementId)?//寻找与item.achievementId相同的，存在于this.data.prop.ach的id，并返回，若存在则不执行，不存在则添加
    '':(data.push(this.data.prop.ach),
      this.setData({
          show: 1,
          prop : this.data.prop//渲染到页面中
          }))
    wx.setStorageSync("achievement", data)//保存到本地
  }
   let that = this
   clearTimeout(timer)//在未执行前清除，不能放到后面，也不能放到定时器里面，会影响效果
   timer = setTimeout(function() {
    that.setData({
      show: 0,
    })
  },2000)
   this.next()
 }
})